<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加角色' : '编辑角色'"
      :visible.sync="dialogInfo.isShow"
    >
      <el-form :rules="rules" :model="roleform" ref="roleInfo">
        <el-form-item
          label="角色名称："
          :label-width="formLabelWidth"
          prop="rolename"
        >
          <el-input v-model="roleform.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限：" :label-width="formLabelWidth">
          <el-tree
            :data="menulist"
            show-checkbox
            node-key="id"
            default-expand-all
            ref="tree"
            :props="{
              children: 'children',
              label: 'title',
            }"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状 态：" :label-width="formLabelWidth">
          <el-switch
            v-model="roleform.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, getRoleInfo, postEditRole } from "../../util/axios";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      formLabelWidth: "100px",
      roleform: {
        rolename: "", //角色名称
        menus: "",
        status: 1,
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" },
        ],
      },
    };
  },
  props: ["dialogInfo"],
  mounted() {
    this.getMenuAction();
  },
  methods: {
    ...mapActions({
      getRoleAction: "role/getRoleListAction",
      getMenuAction: "menu/getMenuListAction",
    }),
    reset() {
      this.roleform = {
        rolename: "", //角色名称
        menus: "",
        status: 1,
      };
      this.$refs.roleInfo.resetFields();
      this.$refs.tree.setCheckedKeys([]);
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(id) {
      getRoleInfo({ id }).then((res) => {
        this.roleform = res.data.list;
        this.$refs.tree.setCheckedKeys(this.roleform.menus.split(","));
        this.roleform.id = id;
      });
    },
    confirm() {
      this.$refs.roleInfo.validate((valid) => {
        if (valid) {
          this.roleform.menus = this.$refs.tree.getCheckedKeys().join(",");
          if (this.dialogInfo.isAdd) {
            addRole(this.roleform).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.cancel();
                this.getRoleAction();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // hahahah
            postEditRole(this.roleform).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.cancel();
                this.getRoleAction();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
      this.cancel()
    },
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenuList",
    }),
  },
};
</script>

<style lang="less" scoped>
</style>
